package com.dragon.controller;

import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.dragon.pojo.Problem.Problem;
import com.dragon.service.DashScopeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("ai")
public class DashScopeController {
    @Autowired
    DashScopeService dashScopeService;

    /**
     * ai智能分析
     * @param recordId 提交记录id
     * @return Flux<String>
     */
    @GetMapping("smart")
    public Flux<String> smartAnalysis(Integer recordId) {
        return dashScopeService.smartAnalysis(recordId);
    }

    /**
     * ai智能出题
     * @param text 用户输入的文本
     * @return Mono<Problem>
     * @throws NoApiKeyException key 不存在异常
     * @throws InputRequiredException 输入异常
     * @throws InterruptedException 中断异常
     */
    @GetMapping("set-question")
    public Mono<Problem> smartSetQuestion(String text) throws NoApiKeyException, InputRequiredException, InterruptedException {
        CompletableFuture<Problem> problemFuture = dashScopeService.smartSetQuestion(text);
        return Mono.fromFuture(problemFuture);
    }
}
